<!doctype html>
<html>

	<head>
		<meta charset="utf-8">
		<title></title>
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<link href="css/mui.css" rel="stylesheet" />
	</head>

	<body>
		<script src="js/mui.js"></script>
		<script type="text/javascript">
			var num_ = 1;

			mui.init({
				beforeback: function() {
					//... //窗口关闭前处理其他业务详情点击 ↑ "关闭页面"链接查看
					scan.close();
				},
			});
			mui.plusReady(function() {
				window.studentSeat = JSON.parse(plus.storage.getItem("studentSeat"));
				window.bjid = plus.storage.getItem("scanbj");
				window.studentSeat["R" + bjid][plus.storage.getItem("studentG").toString()] = {};
				te = JSON.parse(plus.storage.getItem("classList"));
				mui.each(te, function(k, v) {
					//alert(JSON.stringify(v));
					if (v.id == bjid) {
						mui(".mui-title")[0].innerHTML = '初' + v.grade + ' ' + v.alias + "班 第" + plus.storage.getItem("studentG") +
							"排";
					}
				});
				//console.log("当前页面URL：" + plus.webview.currentWebview().getURL());
				startRecognize();
				if (plus.storage.getItem("downLoadStudentInfo") == null) {
					alert('请下载学生信息');
					scan.close();
					window.location.href = 'index.html';
				} else {
					json = JSON.parse(plus.storage.getItem("downLoadStudentInfo"));
					sinfo = {};
					mui.each(json, function(k, v) {
						sinfo['s' + v.ID] = v;
					});
				}


				scan.start();
				fl = false;
			});
			var scan = null;

			function onmarked(type, result) {
				var text = '未知: ';
				switch (type) {
					case plus.barcode.QR:
						text = 'QR: ';
						break;
					case plus.barcode.EAN13:
						text = 'EAN13: ';
						break;
					case plus.barcode.EAN8:
						text = 'EAN8: ';
						break;
					case plus.barcode.CODE128:
						text = 'CODE128: ';
						break;
				}
				//alert(text + result);

				obj = document.getElementById("stuinfo");
				sin = sinfo['s' + result];

				if (sin == null) {
					mui.alert("没有该学生信息\n" + result, "错误!", function() {
						scan.start();
					});
				} else {
					mui.alert(sin.name + "\n" + result, "学生信息", function() {
						scan.start();
					});
					obj.innerHTML = obj.innerHTML + "<div class='mui-card-content mui-col-xs-1'> &nbsp;" + window.num_.toString() +
						"</div><div class='mui-card-content mui-col-xs-5'>初" + sin.grade + sin.class + "班" + sin
						.name +
						"</div><div class='mui-card-content mui-col-xs-6'>" + sin.ID + "</div>";
					studentG = plus.storage.getItem("studentG");
					window.studentSeat["R" + bjid][studentG.toString()][window.num_]={};
					window.studentSeat["R" + bjid][studentG.toString()][window.num_]['id'] = sin.ID;
					window.studentSeat["R" + bjid][studentG.toString()][window.num_]['r'] = 0;
					window.num_ += 1;
				}


			}

			function startRecognize() {
				scan = new plus.barcode.Barcode('bcid', [plus.barcode.QR, plus.barcode.CODE128, plus.barcode.EAN13]);
				scan.onmarked = onmarked;
			}

			function startScan() {
				scan.start();
			}

			function cancelScan() {
				scan.cancel();
			}

			function setFlash() {
				if (fl != true) {
					fl = true;
				} else {
					fl = false;
				};
				scan.setFlash(fl);
			}

			function back_() {
				scan.close();
				history.back(-1);
			}

			function save_() {
				plus.storage.setItem("studentSeat", JSON.stringify(studentSeat));
				mui.alert('保存成功');
				back_();
			}
			/*
			window.onbeforeunload = function(event) {
				scan.close();
			}
			*/
		</script>
		<style type="text/css">
			#bcid {
				background: #FFFFFF;
				height: 360px;
				width: 100%;
				margin: 0 auto;
				text-align: center;
				vertical-align: middle;

			}
			.pa1{
				width:22%;
			}
			.pa2{
				width:53.5%;
			}
		</style>
		<header id="header" class="mui-bar mui-bar-nav">
			<a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left"></a>
			<h1 class="mui-title">导航栏</h1>
		</header>
		<div class="mui-content">
			<div class="mui-content-padded mui-row">
				<div id="bcid"></br></br></br>数据加载中...</div>
			</div>
			<div class="mui-row mui-content-padded">
				<a class="mui-btn pa1" onclick="setFlash()">灯光</a>

				<a class="mui-btn pa2 " onclick="save_()">保存</a>
				<a class="mui-btn pa1" onclick="back_()">返回 </a>
			</div>
			<div id='stuinfo' class="mui-card mui-row">

			</div>
		</div>
	</body>

</html>
